;
// 预报剖面
(function () {
    var profiles = {
    	router: {
            getLatestTime: CafeJS.CXT+'/analysis/getModelLatestTime.do'
        },
        init: function () {
            var that = this;
            $.IDate(".date-panel2", {
				query: function query() {
				}
			});
			this.getLatestTime();
			that.bindEvt();
	 		that.buildLastTime();
        },
        // 获取最新时间
        getLatestTime: function() {
        	var that = this;
        	var pcode = $(".fprobuct-content li.active").data('id');
            var stationId = $(".fprobuct-content1 li.active").data('code');
            var model = $("#modelType li.active").data('code');
            var param = {
                pcode: pcode+'-'+model,
                area: "AFSH",
                stationId: stationId,
                height: 0,
                threshold: 'MEDIAN'
            };
        	$.ajax({
        		url: this.router.getLatestTime,
        		type: 'GET',
        		dataType: 'json',
        		async: false,
        		data: param
        	}).done(function(res){
        		if (res.success && res.params.data) {
        			var latestTime = dateUtil.convertStringToDate7(res.params.data,'yyyy-MM-dd hh:mm');
        			$("#queryDate").IDatePicker().setVal(latestTime);
        		}
        	})
        },
        // 初始化最近起报时间
        buildLastTime: function (call) {
            var baseDate = $("#queryDate").IDatePicker().getVal() + ":00";
            var newDate = new Date(baseDate);
            var liArr = [];
            for (var i = 0; i < 4; i++) {
                liArr.push(`<li class="${i == 0 ? 'active' : ''}" data-time="${newDate}">${newDate.format("ddhh")}</li>`);
                newDate.setHours(newDate.getHours() - 12);
            }
            $(".fnewtime-ct").empty().append(liArr.reverse().join(""));
            $(".fnewtime-ct").children().last().click();
        },
        bindEvt: function () {
            var that = this;
            // 起报时间
            $(".fnewtime-ct").on("click", 'li', function () {
                $(this).addClass("active").siblings().removeClass("active")
                that.getProfileImg()
            })
            // 时效
            $(".fnewtime2-ct").on("click", 'li', function () {
                $(this).addClass("active").siblings().removeClass("active")
                $(".pf-content>li").eq($(this).index()).show().siblings().hide() 
            })
            // 产品名称
            $(".fprobuct-content").on("click", 'li', function () {
                $(this).addClass("active").siblings().removeClass("active")
                that.getProfileImg();
            })
            // 站点切换
            $(".fprobuct-content1").on("click", 'li', function () {
                $(this).addClass("active").siblings().removeClass("active")
                that.getProfileImg()
            })
            $(".pf-title").on("click", 'li', function () {
                $(this).addClass("active").siblings().removeClass("active");
                that.getLatestTime();
                that.buildLastTime();
            })
            $('.date-panel2').off('click').on('click', '.btn-forward', function (e) { // 日期向前
                e.stopPropagation();
                $("#queryDate").IDatePicker().toForward();
            }).on('click', '.btn-backward', function (e) { // 日期向后
                e.stopPropagation();
                $("#queryDate").IDatePicker().toBackward();
            }).on('click', ".btn-search", function (e) { // 搜索
				e.stopPropagation();
				that.buildLastTime();
			}).on('click', ".btn-newest", function (e) { // 最新
                e.stopPropagation();
                $("#queryDate").IDatePicker().toNewest();
                that.buildLastTime();
            });
        },
        // 预报剖面图切换
        getProfileImg: function () {
            var sTime = $(".fnewtime-ct li.active").data('time');
            sTime = new Date(sTime).format('yyyyMMddhhmmss');
            var pcode = $(".fprobuct-content li.active").data('id');
            var stationId = $(".fprobuct-content1 li.active").data('code');
            var model = $("#modelType li.active").data('code');
            var params = {
        		queryTime: sTime,
                pcode: pcode+'-'+model,
                area: "AFSH",
                stationId: stationId
            };
            $.getJSON(CafeJS.CXT + 'analysis/getProfileProduct.do', params, function (res) {
                var divimg = $(".pf-content").empty();
                if (res.success) {
                    var dataArr = JSON.parse(res.params.data);
                    for (var i = 0; i < dataArr.length; i++) {
                        var proObj = dataArr[i];
                        if (proObj.period == 72) continue;
                        var $li = $(`
                        <li>
                        <span>${proObj.description}</span>
                        <div class="pf-content-ct">
                            <i class="pf-content-ct-img">
                            <img src="${proObj.url}">
                            </i>
                        </div>
                        <div class="pf-content-dw">
                            <div class="pf-content-dw-img">
                            <img src="${proObj.legendUrl}">
                            </div>
                        </div>
                    </li>
                        `);
                        $li.data('pro', proObj);
                        divimg.append($li);
                    }
                    $(".pf-content>li").eq($(".fnewtime2-ct .active").index()).show().siblings().hide()
                };
            })
        },
    }
    profiles.init();
})()